// +build darwin dragonfly freebsd linux netbsd openbsd plan9 solaris

package daemon

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"syscall"

	"gddgame.cc/galaxy/utils/lock_file"
)

// A Context describes daemon context.
type Daemon struct {
	// If PidFileName is non-empty, parent process will try to create and lock
	// pid file with given name. Child process writes process id to file.
	PidFileName string
	// Permissions for new pid file.
	PidFilePerm os.FileMode

	// If WorkDir is non-empty, the child changes into the directory before
	// creating the process.
	WorkDir string
	// If Chroot is non-empty, the child changes root directory
	Chroot string

	// If Env is non-nil, it gives the environment variables for the
	// daemon-process in the form returned by os.Environ.
	// If it is nil, the result of os.Environ will be used.
	Env []string
	// If Args is non-nil, it gives the command-line args for the
	// daemon-process. If it is nil, the result of os.Args will be used.
	Args []string

	// Credential holds user and group identities to be assumed by a daemon-process.
	Credential *syscall.Credential
	// If Umask is non-zero, the daemon-process call Umask() func with given value.
	Umask int

	// Struct contains only serializable public fields (!!!)
	abspath  string
	pidFile  *lock_file.LockFile
	nullFile *os.File

	rpipe, wpipe *os.File
}

func (d *Daemon) reborn() (child *os.Process, err error) {
	if !WasReborn() {
		child, err = d.parent()
	} else {
		err = d.child()
	}
	return
}

func (d *Daemon) search() (daemon *os.Process, err error) {
	if len(d.PidFileName) > 0 {
		var pid int
		if pid, err = lock_file.ReadPidFile(d.PidFileName); err != nil {
			return
		}
		// Find Process real
		if err := syscall.Kill(pid, 0); err != nil {
			return nil, errors.New("Process not exist")
		}
		daemon, err = os.FindProcess(pid)
	}
	return
}

func (d *Daemon) parent() (child *os.Process, err error) {
	if err = d.prepareEnv(); err != nil {
		return
	}

	defer d.closeFiles()
	if err = d.openFiles(); err != nil {
		return
	}

	attr := &os.ProcAttr{
		Dir:   d.WorkDir,
		Env:   d.Env,
		Files: d.files(),
		Sys: &syscall.SysProcAttr{
			//Chroot:     d.Chroot,
			Credential: d.Credential,
			Setsid:     true,
		},
	}

	if child, err = os.StartProcess(d.abspath, d.Args, attr); err != nil {
		if d.pidFile != nil {
			_ = d.pidFile.Remove()
		}
		return
	}

	_ = d.rpipe.Close()
	encoder := json.NewEncoder(d.wpipe)
	if err = encoder.Encode(d); err != nil {
		return
	}
	_, err = fmt.Fprint(d.wpipe, "\n\n")
	return
}

func (d *Daemon) openFiles() (err error) {
	if d.PidFilePerm == 0 {
		d.PidFilePerm = FILE_PERM
	}

	if d.nullFile, err = os.Open(os.DevNull); err != nil {
		return
	}

	if len(d.PidFileName) > 0 {
		if d.PidFileName, err = filepath.Abs(d.PidFileName); err != nil {
			return err
		}
		if d.pidFile, err = lock_file.OpenLockFile(d.PidFileName, d.PidFilePerm); err != nil {
			return
		}
		if err = d.pidFile.Lock(); err != nil {
			return
		}
		if len(d.Chroot) > 0 {
			// Calculate PID-file absolute path in child's environment
			if d.PidFileName, err = filepath.Rel(d.Chroot, d.PidFileName); err != nil {
				return err
			}
			d.PidFileName = "/" + d.PidFileName
		}
	}

	d.rpipe, d.wpipe, err = os.Pipe()
	return
}

func (d *Daemon) closeFiles() {
	cl := func(file **os.File) {
		if *file != nil {
			_ = (*file).Close()
			*file = nil
		}
	}
	cl(&d.rpipe)
	cl(&d.wpipe)
	cl(&d.nullFile)
	if d.pidFile != nil {
		_ = d.pidFile.Close()
		d.pidFile = nil
	}
	return
}

func (d *Daemon) prepareEnv() (err error) {
	if d.abspath, err = osExecutable(); err != nil {
		return
	}

	if len(d.Args) == 0 {
		d.Args = os.Args
	}

	mark := fmt.Sprintf("%s=%s", MARK_NAME, MARK_VALUE)
	if len(d.Env) == 0 {
		d.Env = os.Environ()
	}
	d.Env = append(d.Env, mark)

	return
}

func (d *Daemon) files() (f []*os.File) {
	f = []*os.File{
		d.rpipe,    // (0) stdin
		d.nullFile, // (1) stdout
		d.nullFile, // (2) stderr
		d.nullFile, // (3) dup on fd 0 after initialization
	}

	if d.pidFile != nil {
		f = append(f, d.pidFile.File) // (4) pid file
	}
	return
}

var initialized = false

func (d *Daemon) child() (err error) {
	if initialized {
		return os.ErrInvalid
	}
	initialized = true

	decoder := json.NewDecoder(os.Stdin)
	if err = decoder.Decode(d); err != nil {
		return
	}

	// create PID file after context decoding to know PID file full path.
	if len(d.PidFileName) > 0 {
		d.pidFile = lock_file.NewLockFile(os.NewFile(4, d.PidFileName))
		if err = d.pidFile.WritePid(); err != nil {
			return
		}
		defer func() {
			if err != nil {
				_ = d.pidFile.Remove()
			}
		}()
	}

	if err = syscallDup(3, 0); err != nil {
		return
	}

	if d.Umask != 0 {
		syscall.Umask(int(d.Umask))
	}
	if len(d.Chroot) > 0 {
		err = syscall.Chroot(d.Chroot)
		if err != nil {
			return
		}
	}

	return
}

func (d *Daemon) release() (err error) {
	if !initialized {
		return
	}
	if d.pidFile != nil {
		err = d.pidFile.Remove()
	}
	return
}
